package com.jic.product.controller.ce;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.member.constant.consist.ResponseCode;
import com.jic.product.model.entity.MProduct;
import com.jic.product.model.vo.request.MProductRequest;
import com.jic.product.model.vo.response.MProductResponse;
import com.jic.product.service.MProductService;
import com.jic.product.manage.request.ShopInfoRequest;
import com.jic.product.manage.response.ShopInfoResponse;
import com.jic.product.model.vo.request.MProductPriceRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/*
 * @Classname 田雨
 * @Description TODO
 * @Created by 22720
 * 控制层
 */

@RestController
@Slf4j
@RequestMapping("/mProduct")
@Api(tags = " [商品表]控制类-PC")
public class MProductController {

    @Autowired
    private MProductService service;



    /*
     * @Classname 田雨
     * @Description TODO
     * @Created by 22720
     * 新增记录
     */
    @ApiOperation(value = "新增[商品表]记录")
    @PostMapping("/save")
    @ResponseBody
    public RestResult<Long> save(@RequestBody MProductRequest request) throws Throwable {
        MProduct mProduct = new MProduct();
        BeanUtils.copyProperties(request, mProduct);
        long flag = service.save(mProduct);
        return RestResult.success(flag);
    }


    /*
     * @Classname 田雨
     * @Description TODO
     * @Created by 22720
     * 新增或修改记录
     */
    @ApiOperation(value = "保存或更新[商品表]")
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public RestResult<Long> saveOrUpdate(@Validated @RequestBody MProductRequest request) throws Throwable {
        MProduct mProduct = new MProduct();
        BeanUtils.copyProperties(request, mProduct);
        long flag =   service.saveOrUpdate(mProduct);
        return RestResult.success(flag);
    }


    /*
     * @Classname 田雨
     * @Description TODO
     * @Created by 22720
     * 删除记录
     */
    @ApiOperation(value = "删除[商品表]记录")
    @PostMapping("/delete")
    @ResponseBody
    public RestResult<Long> delete(@RequestBody MProductRequest request) throws Throwable {
        MProduct mProduct = new MProduct();
        BeanUtils.copyProperties(request, mProduct);
        long flag = service.deleteByPrimaryKeyLogically(mProduct);
        return RestResult.success(flag);
    }

    /*
     * @Classname 田雨
     * @Description TODO
     * @Created by 22720
     * 修改记录
     */
    @ApiOperation(value = "修改[商品表]记录")
    @PostMapping("/update")
    @ResponseBody
    public RestResult<Long> update(@RequestBody MProductRequest request) throws Throwable {
        MProduct mProduct = new MProduct();
        BeanUtils.copyProperties(request, mProduct);
        long flag = service.updateByPrimaryKeySelective(mProduct);
        return RestResult.success(flag);
    }


    /*
     * @Classname 田雨
     * @Description TODO
     * @Created by 22720
     * 单表查询详情信息
     */
    @PostMapping("/getByPrimaryKey")
    @ApiOperation(value = "查询[商品表]详情")
    @ResponseBody
    public RestResult<MProductResponse> getByPrimaryKey(@Validated  @RequestBody MProductRequest request) {
        log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
        if (request!=null && request.getId()!=null) {

            MProduct mProduct = new MProduct();
            BeanUtils.copyProperties(request, mProduct);
            MProduct result = service.getByPrimaryKey(mProduct);
            log.info("查询详情请求参数出参，{}", JSONObject.toJSONString(result));
            if (result == null) {
                return RestResult.error(ResponseCode.EXCEPTION, "记录不存在");
            }
            MProductResponse bo = new MProductResponse();
            BeanUtils.copyProperties(result, bo);
            return RestResult.success(bo);
        }
        log.info("查询详情请求结束");
        return RestResult.success(new MProductResponse());
    }


    /*
     * @Classname 田雨
     * @Description TODO
     * @Created by 22720
     *  分页查询
     */
    @PostMapping("/listPage")
    @ApiOperation(value = " 分页查询列表 ")
    @ResponseBody
    public RestResult<PageInfo<MProduct>> listPage (@RequestBody MProductRequest request) throws Throwable {
        MProduct param = new MProduct();
        BeanUtils.copyProperties(request, param);
        param.setDeleteFlag(0);
        return RestResult.success(service.listPageBySelective(param, request.getPageNum(),request.getPageSize()));
    }


    /*
     * @Classname 田雨
     * @Description TODO
     * @Created by 22720
     * M_PRODUCT 查询列表（不分页）
     */
    @PostMapping("/listAll")
    @ApiOperation(value = "不分页查询 [商品表] ")
    @ResponseBody
    public RestResult<List<MProductResponse>>  listMProductAllRecord(@RequestBody MProductRequest request) throws Throwable {
        MProduct param = new MProduct();
        BeanUtils.copyProperties(request, param);
        param.setDeleteFlag(0);
        List<MProduct> list = service.listAllRecord(param);
        List<MProductResponse> responseList = list.stream().map(temp -> {
            MProductResponse bo = new MProductResponse();
            BeanUtils.copyProperties(temp, bo);
            return bo;
        }).filter(item -> item != null).collect(Collectors.toList());
        return RestResult.success(responseList);
    }



    /*
     * @Classname 田雨
     * @Description TODO
     * @Created by 22720
     * 修改为下架
     */
    @RequestMapping("/updateA")
    @ApiOperation(value = " 修改为下架 ")
    @ResponseBody
    public RestResult<Integer> updateA(@RequestBody MProductRequest request){
        MProduct param = new MProduct();
        BeanUtils.copyProperties(request, param);
        return RestResult.success(service.updateA(param));

    }



    /*
     * @Classname 田雨
     * @Description TODO
     * @Created by 22720
     * 修改为上架
     */
    @RequestMapping("/updateB")
    @ApiOperation(value = " 修改为上架 ")
    @ResponseBody
    public RestResult<Integer> updateB(@RequestBody MProductRequest request){
        MProduct param = new MProduct();
        BeanUtils.copyProperties(request, param);
        return RestResult.success(service.updateB(param));

    }


    /*
     * @Classname 田雨
     * @Description TODO
     * @Created by 22720
     * 修改协议上浮
     */
    @RequestMapping("/updateListA")
    @ApiOperation(value = " 修改协议上浮 ")
    @ResponseBody
    public RestResult<Integer> updateListA(@RequestBody MProductRequest request){
        MProduct param = new MProduct();
        BeanUtils.copyProperties(request, param);
        return RestResult.success(service.updateListA(param));

    }


    /*
     * @Classname 田雨
     * @Description TODO
     * @Created by 22720
     * 修改微众价
     */
    @RequestMapping("/updateListB")
    @ApiOperation(value = " 修改微众价 ")
    @ResponseBody
    public RestResult<Integer> updateListB(@RequestBody MProductRequest request){
        MProduct param = new MProduct();
        BeanUtils.copyProperties(request, param);
        return RestResult.success(service.updateListB(param));

    }



    /*
     * @Classname 田雨
     * @Description TODO
     * @Created by 22720
     * 查询下架商品
     */
    @PostMapping("/listPageA")
    @ApiOperation(value = " 分页查询下架 ")
    @ResponseBody
    public RestResult<PageInfo<MProduct>> listPageX (@RequestBody MProductRequest request) throws Throwable {
        MProduct param = new MProduct();
        BeanUtils.copyProperties(request, param);
        param.setDeleteFlag(0);
        return RestResult.success(service.listPageX(param, request.getPageNum(),request.getPageSize()));
    }



    /*
     * @Classname 田雨
     * @Description TODO
     * @Created by 22720
     * 查询上架商品
     */
    @PostMapping("/listPageB")
    @ApiOperation(value = " 分页查询上架 ")
    @ResponseBody
    public RestResult<PageInfo<MProduct>> listPageS (@RequestBody MProductRequest request) throws Throwable {
        MProduct param = new MProduct();
        BeanUtils.copyProperties(request, param);
        param.setDeleteFlag(0);
        return RestResult.success(service.listPageS(param, request.getPageNum(),request.getPageSize()));
    }
    @PostMapping("/listPointsProduct")
    @ApiOperation(value = "查询积分可兑换商品/通过品牌查询商品")
    @ResponseBody
    public RestResult<PageInfo<ShopInfoResponse>> listPointsProduct(@RequestBody ShopInfoRequest request){
        return RestResult.success(service.listPointsProduct(request,request.getPageNum(),request.getPageSize()));
    }

    /*
     * @Classname 李学会
     * @Description TODO
     * @Created by 22720
     *  分页查询
     */
    @PostMapping("/topTenProduct")
    @ApiOperation(value = " 查询 ")
    @ResponseBody
    public RestResult<PageInfo<MProduct>> topTenProduct (@RequestBody MProductRequest request) throws Throwable {
        MProduct param = new MProduct();
        BeanUtils.copyProperties(request, param);
        param.setDeleteFlag(0);
        return RestResult.success(service.topTenProductQuery(param, request.getPageNum(),request.getPageSize()));
    }

}
